package com.example.mitsuki_momo_livestreamingschedule.util;

import com.example.mitsuki_momo_livestreamingschedule.model.Event;
import com.example.mitsuki_momo_livestreamingschedule.model.PublicEvents;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author ：lxb
 * @ProjectName: mitsuki_momo_liveStreamingSchedule
 * @Package: com.example.mitsuki_momo_livestreamingschedule.util
 * @ClassName: TxtBuildUtil
 * @date ：Created in  2023/3/7 15:50
 * @description：构建ics文件
 * @version: 1.0
 */
public class TxtBuildUtil {

    public static void buildIcs(PublicEvents publicEvents){
        try {

            File file = new File(".\\ics\\美月momo直播日历.ics");
            if (file.exists()) {
                System.out.println("File exists");
            } else {
                System.out.println("File not exists, create it ...");
                //getParentFile() 获取上级目录(包含文件名时无法直接创建目录的)
                if (!file.getParentFile().exists()) {
                    System.out.println("not exists");
                    //创建上级目录
                    file.getParentFile().mkdirs();
                }
                try {
                    //在上级目录里创建文件
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileWriter write=new FileWriter(file);
            BufferedWriter bw=new BufferedWriter(write);
            bw.write("BEGIN:VCALENDAR\n" +
                    "PRODID:-//美月momo//美月momo 直播日程表//CN\n" +
                    "VERSION:2.0\n" +
                    "CALSCALE:GREGORIAN\n" +
                    "METHOD:PUBLISH\n" +
                    "X-WR-CALNAME:美月momo 直播日程表\n" +
                    "X-WR-TIMEZONE:Asia/Shanghai\n" +
                    "X-WR-CALDESC:美月momo 直播日程表，由 @0不知语0 制作\n" +
                    "BEGIN:VTIMEZONE\n" +
                    "TZID:Asia/Shanghai\n" +
                    "X-LIC-LOCATION:Asia/Shanghai\n" +
                    "BEGIN:STANDARD\n" +
                    "TZOFFSETFROM:+0800\n" +
                    "TZOFFSETTO:+0800\n" +
                    "TZNAME:CST\n" +
                    "DTSTART:19700101T000000\n" +
                    "END:STANDARD\n" +
                    "END:VTIMEZONE\n");

            for (Event event : publicEvents.getPublicEvents()){
                bw.write("BEGIN:VEVENT\n");
                bw.write("DTSTAMP;TZID=Asia/Shanghai:"+TimeUtil.timeToTType(event.getStart_at())+"\n");
                bw.write("UID:美月momo"+TimeUtil.timeToTType(event.getStart_at())+"@bilibili.com\n");
                bw.write("DTSTART;TZID=Asia/Shanghai:"+TimeUtil.timeToTType(event.getStart_at())+"\n");
                bw.write("DURATION:PT"+TimeUtil.getTimeDifference(event.getStart_at(),event.getEnd_at())+"M\n");
                bw.write("SUMMARY:"+event.getTitle()+"\n");
                bw.write("URL:"+event.getLocation_url()+"\n");
                if (!"".equals(event.getLocation_url())){
                    bw.write("DESCRIPTION:"+event.getLocation_url()+"\\n点击上方链接打开哔哩哔哩，进入直播间\n");
                }else {
                    bw.write("DESCRIPTION: \n");
                }
                bw.write("LOCATION:"+event.getOverview()+"\n");
                bw.write("BEGIN:VALARM\n");
                bw.write("ACTION:DISPLAY\n");
                bw.write("DESCRIPTION:直播即将开始\n");
                bw.write("TRIGGER:-PT10M\n");
                bw.write("END:VALARM\n");
                bw.write("END:VEVENT\n");
            }

            bw.write("END:VCALENDAR\n");
            bw.close();
            write.close();
        }catch(IOException e){
            e.printStackTrace();
        }

    }

}
